package cn.ekuma.epos.datalogic.define.dao;

import cn.ekuma.data.dao.ModifiedLogDAO;
import cn.ekuma.epos.db.table.I_ProductLocationState;
import com.openbravo.bean.Product;
import com.openbravo.bean.ProductLocationState;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.DataParams;
import com.openbravo.data.loader.DataRead;
import com.openbravo.data.loader.DataWrite;
import com.openbravo.data.loader.DataWriteUtils;
import com.openbravo.data.loader.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.PreparedSentence;
import com.openbravo.data.loader.SerializerReadDouble;
import com.openbravo.data.loader.SerializerReadInteger;
import com.openbravo.data.loader.SerializerWriteParams;
import com.openbravo.data.loader.TableDefinition;
import com.openbravo.data.model.Field;
import com.openbravo.format.Formats;

public class ProductLocationStateDAO extends ModifiedLogDAO<ProductLocationState>{
   
	public ProductLocationStateDAO(I_Session s) {
        super(s);
    }

    @Override
    public TableDefinition getTable() {
        return new TableDefinition(s,
                   "PRODUCT_LOCATION_STATE",
                    new Field[]{
                new Field(I_ProductLocationState.ID, Datas.STRING, Formats.STRING),
                new Field(I_ProductLocationState.PRODUCT_ID, Datas.STRING, Formats.STRING,Product.class),
                new Field(I_ProductLocationState.LOCATION_ID, Datas.STRING, Formats.STRING),
                new Field(I_ProductLocationState.STATE, Datas.INT, Formats.INT),
                new Field(I_ProductLocationState.LASTMODIFIED,Datas.TIMESTAMP, Formats.TIMESTAMP)
                },
                new int[]{0}
        );
    }

	@Override
	public void writeInsertValues(DataWrite dp, ProductLocationState obj)
			throws BasicException {
		dp.setString(1,obj.getId());
		dp.setString(2,obj.getProductId());
		dp.setString(3,obj.getLocationId());
		dp.setInt(4,obj.getState());
		dp.setTimestamp(5,obj.getLastModified());
		
	}

	@Override
	public Class getSuportClass() {
		return ProductLocationState.class;
	}

	@Override
	public ProductLocationState readValues(DataRead dr, ProductLocationState obj)
			throws BasicException {
		if(obj==null)
			obj=new ProductLocationState();
		obj.setId(dr.getString(1));
		obj.setProductId(dr.getString(2));
		obj.setLocationId(dr.getString(3));
		obj.setState(dr.getInt(4));
		obj.setLastModified(dr.getTimestamp(5));
		return obj;
	}
	
	
	public boolean isProductOutAble(String locationId,String product)throws BasicException {
		if( !isStateAble(locationId,product)||isProductInState(locationId,product,new Integer[]{ProductLocationState.STATE_INOUT,ProductLocationState.STATE_OUT}))
			return true;
		return false;
	}
	
	public boolean isProductLocked(String locationId,String product)throws BasicException {
		if(isProductInState(locationId,product,new Integer[]{ProductLocationState.STATE_LOCK}))
			return true;
		return false;
	}
	
	public boolean isProductInAble(String locationId,String product)throws BasicException {
		if( !isStateAble(locationId,product)||isProductInState(locationId,product,new Integer[]{ProductLocationState.STATE_INOUT,ProductLocationState.STATE_IN}))
			return true;
		return false;
	}
	
    public boolean isProductInState(final String locationId,final String product,Integer[] state)throws BasicException {
    	int count=(Integer) new PreparedSentence(
				s,
				getTable().getCountSql()+" WHERE LOCATION_ID=? AND PRODUCT_ID=? AND STATE IN("+DataWriteUtils.getSQLValue(state)+")",
				SerializerWriteParams.INSTANCE,SerializerReadInteger.INSTANCE).find(new DataParams() {
			@Override
			public void writeValues() throws BasicException {
				setString(1, locationId);
				setString(2, product);
			}
		});
    	return count>0;
	}
    
    public boolean isStateAble(final String locationId,final String product)throws BasicException {
    	int count=(Integer) new PreparedSentence(
				s,
				getTable().getCountSql()+" WHERE LOCATION_ID=? AND PRODUCT_ID=? ",
				SerializerWriteParams.INSTANCE,SerializerReadInteger.INSTANCE).find(new DataParams() {
			@Override
			public void writeValues() throws BasicException {
				setString(1, locationId);
				setString(2, product);
			}
		});
    	return count>0;
    }
    
    public void insertOrUpdateProductState(final String locationId,final String product,final int state)throws BasicException {
    	int updateresult;
    	updateresult = new PreparedSentence(
				s,
				"UPDATE PRODUCT_LOCATION_STATE SET STATE =? WHERE LOCATION_ID=? AND PRODUCT_ID=? ",
				SerializerWriteParams.INSTANCE).exec(new DataParams() {
			@Override
			public void writeValues() throws BasicException {
			    this.setInt(1, state);
				setString(2, locationId);
				setString(3, product);

			}
		});
    	if (updateresult == 0) {
    		ProductLocationState obj=new ProductLocationState();
    		obj.setLocationId(locationId);
    		obj.setProductId(product);
    		obj.setState(state);
    		insert(obj);
    	}
    }
}
